$(function() {
	
	// 定义列
	var columns = [
		{
				field : 'no',
				title : '序号',
				align : 'left',
				valign: 'middle',
				formatter : function(value, row, index) {
					return index + 1;
				}
		},
		{
			field : 'customerName',
			title : '客户姓名',
			align : 'center',
			valign: 'middle'
		},
		{
			field : 'companyName',
			title : '单位名称',
			align : 'center',
			valign: 'middle'
		},
		{
			field : 'receiverName',
			title : '收货人',
			align : 'center',
			valign: 'middle'
		},
		{
			field : 'mobile',
			title : '联系电话',
			align : 'center',
			valign: 'middle'
		},
		{
			field : '#',
			title : '收货地址',
			align : 'left',
			halign: 'center',
			valign: 'middle',
			formatter: function(value, row, index){
				return row.provinceName + row.cityName + row.areaName + row.streetName + row.address;
			}
		}, 
		{
			field : 'postCode',
			title : '邮政编码',
			align : 'center',
			valign: 'middle'
		},
		{
			field : 'createTime',
			title : '创建时间',
			align : 'center',
			valign: 'middle',
			formatter: function(value, row, index) {
				return moment(value).format('YYYY-MM-DD HH:mm:ss')
			}
		},
		{
			field : 'isDefault',
			title : '是否默认',
			width: 40,
			align : 'center',
			valign: 'middle',
			formatter: function(value, row, index) {
				if (value == '1') {
					return "<span class='label label-success'>默认地址</span>";
				} else {
					return "";
				}
			}
		},

		{	
			field : '#',
			title : '操作',
			align : 'center',
			width: '135px',
			valign: 'middle',
			formatter : function(value, row, index) {
				var strHtml = "";
   				strHtml += "<a class='btn btn-warning btn-xs' onclick='toEdit("+row.id+")'>编辑</a>";
   				strHtml += "<a class='btn btn-danger btn-xs' onclick='toRemove("+row.id+")'>删除</a>";
   				return strHtml;
			}
		}, ];
			
			
	// 定义查询回调函数（查询参数在此设置）
	var queryParams = function(params) {
	
		var customerName =  $("#customerName").val()
		var companyName =  $("#companyName").val()
		var receiverName =  $("#receiverName").val()
		var mobile =  $("#mobile").val()
		var receiverAddr =  $("#receiverAddr").val() 
		
		return {
			rows : params.limit, // 页面大小
			page : params.pageNumber, // 页码
			customerName : customerName,
			companyName : companyName,
			receiverName : receiverName,
			mobile : mobile,
			receiverAddr : receiverAddr, 
		};
	};
	
	var url = rootPath + "/deliveryAddress/listDeliveryAddressPage"; // 请求json数据的服务器地址
	sys.initTable('deliveryAddressTable', {
		url : url,
		queryParams : queryParams,
		columns : columns,
		striped : true,
		clickToSelect : true
	});

	// 查询
	$('#btnQuery').on('click', function() {
		sys.reloadTable('deliveryAddressTable');
	});
	
	// 清空
	$('#btnClear').on('click', function() {
		$("#customerName").val("");
		$("#companyName").val("");
		$("#receiverName").val("");
		$("#mobile").val("");
		$("#receiverAddr").val("");
	});

	$('#btnAdd').on('click', function() {
		window.location.href = rootPath + '/deliveryAddress/toAddDeliveryAddress';
	});

})

// 跳转编辑页面
function toEdit(id) {
	window.location.href = rootPath + "/deliveryAddress/toUpdateDeliveryAddress?id=" + id;
}

function toRemove(id) {
	var url = rootPath + "/deliveryAddress/removeDeliveryAddress";
	showConfirm("确定要删除？", function(){
		$.ajax({
			type: 'POST',
			url: url,
			async: false,
			data: {id: id},
			dataType: 'json',
			success: function(data) {
				if (data.resultCode == '1') {
					//sys.reloadTable("deliveryAddressTable");
					$("#deliveryAddressTable").bootstrapTable("remove",{field: 'id', values: [id]});
				} else {
					showErr(data.msg);
				}
			}
		});
	})
}